//SOON: COMMENTS ADDED IN CAP ARE BY SOON
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "storage.h"
#include "utils.h"
#include <time.h>

int doLogging =2;	//Logging variable

int main(int argc, char *argv[]) {

    int exitLoop = 0;			//Initialize variables
    int selection = 0;
    void *conn = NULL;
    int status = 0;
    struct storage_record *r;

    char serverhost[100];
    int serverport = 0;
    char username[100];
    char password[100];
    char key[100];
    char table[100];

    if(doLogging==2){			//Log to file?
    time_t basetime;
    struct tm * conTime;
    char cFile[32];

    time(&basetime);
    //SOON: THIS IS AWESOME, I DIDN'T KNOW YOU COULD DO THIS
    conTime = localtime(&basetime);
    //SOON: I AM NOT SURE IF THIS IS RIGHT, CAUSE YOU WILL NEED TO APPEND 0'S IN FRONT OF SINGLE DIGIT NUMBERS ESPECIALLY (DATE)
    strftime(cFile, 32, "Client-%Y-%m-%d-%H-%M-%S.log",conTime);
    //Generate file name
    clientFile = fopen(cFile, "w");	//Open file
    }

    //SOON: PREFERRED IF THIS WAS IN A SINGLE STATEMENT. SHOULD NEVER HAVE 2 PRINTF CONSEQUENTLY
    printf("> ---------------------\n");			//Initial parser
    printf("> 1) Connect\n");
    printf("> 2) Authenticate\n");
    printf("> 3) Get\n");
    printf("> 4) Set\n");
    printf("> 5) Disconnect\n");
    printf("> 6) Exit\n");
    printf("> ---------------------\n");

    while (!exitLoop) {
        printf("> Please enter your selection: ");	//Prompt for response
        scanf("%d", &selection);

	// SOON: DEFINITELY SHOULD USE SWITCH STATEMENTS HERE SINCE LOTS OF ELSE IF
        if (selection == 1) {	//Connection

            printf("> Please input the hostname: ");
            scanf("%s", serverhost);
            printf("> Please input the port: ");
            scanf("%d", &serverport);

            // Connect to server
            conn = storage_connect(serverhost, serverport);
            if (!conn) {
                printf("Cannot connect to server @ %s:%d. Error code: %d.\n",
                        serverhost, serverport, errno);
		// SOON: NOT SURE IF ITS POSSIBLE TO CLOSE ANY FILE BY PASSING IN THE FILE'S NUMBER AS A PARAMETER
		// SOON: BUT THIS DEFINITELY WONT WORK FOR MILESTONE 4 WHEN WE HAVE MULTIPLE CLIENTS
                closeC(doLogging);
                return -1;
            }
            continue;
        }

        else if (selection == 2) {	//Authenticate

            printf("> Please enter your username: ");
            scanf("%s", username);
            printf("> Please enter your password: ");
            scanf("%s", password);

            // Authenticate the client.
            status = storage_auth(username, password, conn);
            if (status != 0) {
                printf("storage_auth failed with username '%s' and password '%s'. " \
                        "Error code: %d.\n", username, password, errno);
                storage_disconnect(conn);
                closeC(doLogging);
                return status;
            }
            printf("storage_auth: successful.\n");
            continue;
        }

        else if (selection == 3) {	//Set

            printf("> Please enter the key: ");
            scanf("%s", key);
            printf("> Please enter the table name: ");
            scanf("%s", table);

            // Issue storage_set
            r = (struct storage_record*) malloc(sizeof (struct storage_record));
            strncpy(r->value, "some_value", sizeof r->value);
            status = storage_set(table, key, r, conn);
            if (status != 0) {
                printf("storage_set failed. Error code: %d.\n", errno);
                storage_disconnect(conn);
                free(r);
                r = NULL;
                closeC(doLogging);
                return status;
            }
            printf("storage_set: successful.\n");
            continue;
        }

        else if (selection == 4) {	//Get

            printf("> Please enter the key: ");
            scanf("%s", key);
            printf("> Please enter the table name: ");
            scanf("%s", table);

            // Issue storage_get
            status = storage_get(table, key, r, conn);
            if (status != 0) {
                printf("storage_get failed. Error code: %d.\n", errno);
                storage_disconnect(conn);
                closeC(doLogging);
                return status;
            }
            printf("storage_get: the value returned for key '%s' is '%s'.\n",
                    key, r->value);
            continue;
        }

        else if (selection == 5) {	//Disconnect
            // Disconnect from server
            status = storage_disconnect(conn);
            if (status != 0) {
                printf("storage_disconnect failed. Error code: %d.\n", errno);
                closeC(doLogging);
                return status;
            }
            continue;
        }

        else if (selection == 6) {	//Exit
            closeC(doLogging);
            return 0;
        }

        else {						//Invalid selection
            printf("Invalid input, exiting.\n");
            closeC(doLogging);
            return 0;
        }


    }
    // Exit
    return 0;
}
